﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Odyssey.Linq;
using Odyssey.Web.Models;
using SunTzu.Utility;
using SunTzu.Core.Data;
using System.Web.Routing;

namespace Odyssey.Web.Areas.Admin.Controllers
{
    public class ArticleController : BaseController<tb_article>
    {
        [ValidateInput(false)]
        public override ActionResult Save(int? id)
        {
            return base.Save(id);
        }

        private int? ClassId
        {
            get
            {
                int classId;
                if (int.TryParse(TempData["classid"] as string ?? Request.QueryString["classid"], out classId))
                {
                    return classId;
                }
                return null;
            }
        }

        protected override void BeforeSaveOrUpdate(tb_article entity)
        {
            entity.IsHot = Request["IsHot"].ParseToBool();
            entity.ClassId = Request["subclass"].ParseToInt();
            entity.TopClassId = CacheHelper<tb_subclass>.GetById(entity.ClassId).TopClassId;
            TempData["classid"] = entity.IsNews ? 1 : 2;
        }

        public override ActionResult List(int? page)
        {
            if (ClassId.GetValueOrDefault() == 1)
            {
                ViewData.Model = table.Where(c => c.IsNews)
                   .OrderByDescending(c => c.Id)
                   .ToPagedList(page.GetValueOrDefault(), numPerPage);
                return View();
            }
            else
            {
                ViewData.Model = table.Where(c => !c.IsNews)
                   .OrderByDescending(c => c.Id)
                   .ToPagedList(page.GetValueOrDefault(), numPerPage);
                return View();
            }
        }

        [ValidateInput(false)]
        public override ActionResult Edit(int? id)
        {
            if (ClassId.GetValueOrDefault() == 1)
            {
                ViewBag.IsNews = true;
                ViewData.Add("parentsubclass", new List<tb_subclass>());
                ViewData.Add("subclass", GetTable<tb_subclass>().Where(c => c.TopClassId == ClassId && c.isnews == "Y").ToList());
            }
            else
            {
                ViewBag.IsNews = false;
                ViewBag.SubClass = CacheHelper<tb_subclass>.GetAll().Where(c => c.isnews == "N");
                //ViewData.Add("parentsubclass", GetTable<tb_subclass>().Where(c => c.isnews == "N" && c.parentid == 0).ToList());
                //ViewData.Add("subclass", GetTable<tb_subclass>().Where(c => c.isnews == "N" && c.parentid != 0).ToList());
            }
            return base.Edit(id);
        }

        protected override void AfterModelLoad(tb_article entity)
        {
            ViewBag.ModelClass = GetTable<tb_subclass>().FirstOrDefault(item => item.Id == entity.ClassId) ?? new tb_subclass();
            base.AfterModelLoad(entity);
        }

        protected override void ProcessRouteValues(RouteValueDictionary routeValues, tb_article article)
        {
            var subclass = CacheHelper<tb_subclass>.GetById(article.ClassId);
            routeValues.Add("classid", subclass.TopClassId);
        }
    }
}

